package sedev.shopping.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import sedev.shopping.entities.BinhLuan;
import sedev.shopping.entities.ListBinhLuan;
import sedev.shopping.util.*;

public class BinhLuanDAO {
	
	public BinhLuanDAO(){
		
	}
	
	private BinhLuan initData(ResultSet result_set){
		BinhLuan data = new BinhLuan();
		try{
			data.setId(result_set.getLong(BinhLuan.Properties.id.name()));
        }catch(SQLException sqle){
        	data.setId(Long.valueOf(0));
        }
        try{
        	data.setBinhLuanId(result_set.getLong(BinhLuan.Properties.binh_luan_id.name()));
        }catch(SQLException sqle){
        	data.setBinhLuanId(Long.valueOf(0));
        }
        try{
        	data.setUserId(result_set.getLong(BinhLuan.Properties.user_id.name()));
        }catch(SQLException sqle){
        	data.setUserId(Long.valueOf(0));
        } 
        try{
        	data.setSanPhamId(result_set.getLong(BinhLuan.Properties.san_pham_id.name()));
        }catch(SQLException sqle){
        	data.setSanPhamId(Long.valueOf(0));
        }
        try{
        	data.setNoiDung(result_set.getString(BinhLuan.Properties.noi_dung.name()));
        }catch(SQLException sqle){
        	data.setNoiDung("");
        }
        try{
        	data.setNgayDang(result_set.getLong(BinhLuan.Properties.ngay_dang.name()));
        }catch(SQLException sqle){
        	data.setNgayDang(Long.valueOf(0));
        }
		return data;
	}

	public boolean addData(BinhLuan data){
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();	
			session.save(data);			
			transaction.commit();
		}catch(HibernateException ex){
			if(transaction != null){
				transaction.rollback();				
			}
			return false;		
		}
		return true;
	}
	
	public boolean updateData(BinhLuan data){
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try{
			transaction = session.beginTransaction();	
			session.update(data);			
			transaction.commit();
		}catch(HibernateException ex){
			if(transaction != null){
				transaction.rollback();				
			}
			return false;		
		}
		return true;
	}
	
	public boolean removeData(Long id){
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;		
		try{
			transaction = session.beginTransaction();			
			BinhLuan data = new BinhLuan();
			data.setId(id);
			session.delete(data);
			transaction.commit();
		}catch(HibernateException ex){
			if(transaction != null){
				transaction.rollback();				
			}
			return false;		
		}		
		return true;
	}
	
	public BinhLuan getData(Long id){
		BinhLuan data = new BinhLuan();		
		Session session = HibernateUtil.getSession().openSession();		
		try{
			session.load(data,id);		
		}catch(HibernateException ex){
			data = null;
		}
		return data;
	}
	
	public BinhLuan getData(Long id, String condition){
		BinhLuan data = new BinhLuan();		
		Connector conn = Connector.getInstance();
		StringBuilder sql = new StringBuilder();
		sql.append("select ").append("*");
		sql.append(" from ").append(BinhLuan.Table);
		sql.append(" where ").append("id = ").append(id);
		if(condition != null && !condition.equals("")){
			sql.append(" and ").append(condition);
		}
		
		ResultSet rs = conn.executeQuery(sql.toString());		
		try{
            if(rs != null && rs.next()){
            	data = this.initData(rs);                
            }
        }catch(SQLException sqle){
        	data = null;
        }
		return data;
	}
	
	public ListBinhLuan getListData(String field, String condition, String order, Paging paging){
		ListBinhLuan listdata = new ListBinhLuan();			
		Connector conn = Connector.getInstance();
		
		StringBuilder sql = new StringBuilder();
		sql.append("select ").append(field);
		sql.append(" from ").append(BinhLuan.Table);
		sql.append(" where ").append(condition);
		if(order!=null && !order.equals("")){
			sql.append(" order by ").append(order);
		}
		if(paging.getPageSize() > 0){
			paging.setTotalRecord(conn.getTotalRecord(BinhLuan.Table, condition));
			paging.checkPage();            
            Long offset = paging.getCurrent().longValue()-1;
            offset = offset * paging.getPageSize();
            sql.append(" limit ").append(offset).append(",").append(paging.getPageSize());
        }
		
		ResultSet rs = conn.executeQuery(sql.toString());		
		try{
            if(rs != null){
                while(rs.next()){                   
                   listdata.add(this.initData(rs));
                }
            }
        }catch(SQLException sqle){
        	listdata.clear();
        }
		return listdata;
	}
}
